/*
 *  author: Honrun
 */
#include "stm32f4xx_hal.h"
#include "stdio.h"
#include "DevicesRandom.h"


/* RNG handler declaration */
RNG_HandleTypeDef g_typeRngHandle;


void vRandHardInit(void)
{
    /* RNG Peripheral clock enable */
    __HAL_RCC_RNG_CLK_ENABLE();

    g_typeRngHandle.Instance = RNG;

    if(HAL_RNG_Init(&g_typeRngHandle) != HAL_OK)
    {
        /* Initialization Error */
        printf("HAL_RNG_Init error.\r\n");
    }
}

void vSrandHard(uint32_t uiRandSeed)
{
    (void)uiRandSeed;
}

uint32_t uiRandHard(void)
{
    uint32_t uiRand = 0;

    uiRand = HAL_RNG_GetRandomNumber(&g_typeRngHandle);

    return uiRand;
}




static uint32_t st_uiRandSeed = 123;


void vSrandSoft(uint32_t uiRandSeed)
{
    st_uiRandSeed = uiRandSeed;
}

uint32_t uiRandSoft(void)
{
    const uint32_t uiMultiplier = 1103515245, uiIncrement = 12345;

    /* 线性同余 */
    st_uiRandSeed = st_uiRandSeed * uiMultiplier + uiIncrement;

    return st_uiRandSeed;
}
